/*
*   学员信息页面
*/
angular.module('jisuStuHome').controller('PhotoImg', ['FileUploader', '$scope', 'StuRequestTrain', 'requestStatisticService', 'sysUserInfo', 'Msg', 'MsgCon', 'T', function (FileUploader, $scope, ST, RSS, sysUserInfo, Msg, MsgCon, T) {

    var video  =null
    var canvas = null
    var context  = null

    video =  document.getElementById('video')
    canvas = document.getElementById('canvas')
    context = canvas.getContext('2d');
   

    $scope.photoImg=function(){

        context.drawImage(video, 0, 0, 440, 320);
        var imgData = canvas.toDataURL();
        var userinfo =sysUserInfo.getuserinfo();

        var param={
            faceId: userinfo.user_ID,
            uid:userinfo.user_ID,
            xid:userinfo.user_ID,
            xtype:6,
            isContrast:false,
            blob:imgData
        };

        ST.saveface(param).then(function (response) {

                if (typeof (response) == "string") {
                    response = JSON.parse(response);
                }
                if (response.data.errorcode == 0) {
                    var imgsrc = response.data.data.facePhoto + "?data=" + new Date().getTime();
                    $(".image_content_rightt").html("<img src=\"" + imgsrc + "\" style=\"width:100%;height:100%;\">");
                    //重新放回缓存
                    if (userinfo.userContact != undefined) {
                        userinfo.userContact.facePhoto = imgsrc;
                        sysUserInfo.sysUserInfo(userinfo);
                        Msg.success("保存成功");
                    }
                } else if(response.data.errorcode!=6&&response.data.errormsg!=null&&response.data.errormsg.length>0){
                    Msg.warning(response.data.errormsg);
                } else {
                    Msg.error("获取数据失败！"+response.data.errormsg);
                }
        });
    }

    $scope.getUserMedia=function (success, error) {
        constraints={video : {width: 440, height: 320},audio:false};
        if (navigator.mediaDevices.getUserMedia) {
            navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);//最新的标准API
        } else if (navigator.webkitGetUserMedia) {
            navigator.webkitGetUserMedia(constraints,success, error)//webkit核心浏览器
        } else if (navigator.mozGetUserMedia) {
            navigator.mozGetUserMedia(constraints, success, error);//firfox浏览器
        } else if (navigator.getUserMedia) {
            navigator.getUserMedia(constraints, success, error);//旧版API
        }
    }
    $scope.success=function (stream) {
        let CompatibleURL = window.URL || window.webkitURL;
        video.srcObject = stream;
        video.play();
    }
    $scope.error=function error(error) {
        console.log(`访问用户媒体设备失败${error.name}, ${error.message}`);
    }

    if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
        $scope.getUserMedia($scope.success, $scope.error);//调用用户媒体设备, 访问摄像头
    } else {
        Msg.warning('不支持访问用户媒体');
    }
	 var userinfo =sysUserInfo.getuserinfo();
	if(userinfo.userContact!=undefined&&userinfo.userContact.facePhoto!=undefined){
		var imgsrc = userinfo.userContact.facePhoto + "?data=" + new Date();
		$(".image_content_rightt").html("<img src=\"" + imgsrc + "\" style=\"width:100%;height:100%;\">");
	}


}])